var express = require('express');
var router = express.Router();
const mailer = require('../../service/sendMail');
/**
 * @api {post} /api/mail/send/single 发送邮件(单个)
 * @apiGroup mail
 * @apiVersion 1.0.0
 * @apiName single
 * @apiParam {String} [from] 发送账户名
 * @apiParam {String} [pass] 发送密码/授权码
 * @apiParam {String} [to] 收件人邮箱
 * @apiParam {String} [cc] 抄送(非必填)
 * @apiParam {String} [subject] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Boolean} [isHtml] 是否标签页(非必填)
 * @apiParam {String} [config] 其他配置
 * @apiSuccessExample {json} Success-Response:
 *  HTTP/1.1 200 OK
 * {
 *  "code": 200,
 *  "message": "发送成功",
 * }
 * @apiErrorExample {json} Error-Response:
 *  HTTP/1.1 500 error
 * {
 *  "code": 500,
 *  "message": "操作失败",
 * }
 * @apiSampleRequest /api/mail/send/single
 */
router.post('/send/single', async function(req, res, next) {
    const reqData = req.body;
    try {
        await mailer.sendMail(reqData);
        return res.json({ code: 200, message: "发送成功" });
    } catch (e) {
        console.error('send single error: ', e);
        return res.json({ code: 500, message: "操作失败" });
    }
});

/**
 * @api {post} /api/mail/send/email 发送邮件
 * @apiGroup mail
 * @apiVersion 1.0.0
 * @apiName email
 * @apiParam {String} [from] 发送账户名
 * @apiParam {String} [pass] 发送密码/授权码
 * @apiParam {String} [to] 收件人邮箱
 * @apiParam {String} [cc] 抄送(非必填)
 * @apiParam {String} [subject] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Boolean} [isHtml] 是否标签页(非必填)
 * @apiSuccessExample {json} Success-Response:
 *  HTTP/1.1 200 OK
 * {
 *  "code": 200,
 *  "message": "发送成功",
 * }
 * @apiErrorExample {json} Error-Response:
 *  HTTP/1.1 500 error
 * {
 *  "code": 500,
 *  "message": "操作失败",
 * }
 * @apiSampleRequest /api/mail/send/email
 */
router.post('/send/email', async function(req, res, next) {
    const reqData = req.body;
    try {
        const resp = await mailer.gmailSend(reqData);
        console.log(resp);
        return res.json(resp);
    } catch (e) {
        console.error('send single error: ', e);
        return res.json({ code: 500, message: "操作失败" });
    }
});

/**
 * @api {post} /api/mail/send/gmail 发送邮件(谷歌)
 * @apiGroup mail
 * @apiVersion 1.0.0
 * @apiName gmail
 * @apiParam {String} [from] 发送账户名
 * @apiParam {String} [pass] 发送密码/授权码
 * @apiParam {String} [to] 收件人邮箱(多个使用,隔开)
 * @apiParam {String} [cc] 抄送(非必填)
 * @apiParam {String} [subject] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Boolean} [isHtml] 是否标签页(非必填)
 * @apiSuccessExample {json} Success-Response:
 *  HTTP/1.1 200 OK
 * {
 *  "code": 200,
 *  "message": "发送成功",
 * }
 * @apiErrorExample {json} Error-Response:
 *  HTTP/1.1 500 error
 * {
 *  "code": 500,
 *  "message": "操作失败",
 * }
 * @apiSampleRequest /api/mail/send/gmail
 */
router.post('/send/gmail', async function(req, res, next) {
    const reqData = req.body;
    try {
        await mailer.gmailSend(reqData);
        return res.json({ code: 200, message: "发送成功" });
    } catch (e) {
        console.error('send single error: ', e);
        return res.json({ code: 500, message: "操作失败" });
    }
});

/**
 * @api {post} /api/mail/send/batch 发送邮件(批量)
 * @apiGroup mail
 * @apiVersion 1.0.0
 * @apiName batch
 * @apiParam {String} [from] 发送账户名
 * @apiParam {String} [pass] 发送密码/授权码
 * @apiParam {String} [to] 收件人邮箱(多个使用,隔开)
 * @apiParam {String} [cc] 抄送(非必填)
 * @apiParam {String} [subject] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Boolean} [isHtml] 是否标签页(非必填)
 * @apiSuccessExample {json} Success-Response:
 *  HTTP/1.1 200 OK
 * {
 *  "code": 200,
 *  "message": "发送成功",
 * }
 * @apiErrorExample {json} Error-Response:
 *  HTTP/1.1 500 error
 * {
 *  "code": 500,
 *  "message": "操作失败",
 * }
 * @apiSampleRequest /api/mail/send/batch
 */
router.post('/send/batch', async function(req, res, next) {
    const reqData = req.body;
    try {
        await mailer.processSendMail(reqData);
        return res.json({ code: 200, message: "发送成功" });
    } catch (e) {
        console.error('send single error: ', e);
        return res.json({ code: 500, message: "操作失败" });
    }
});


module.exports = router;
